home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
gamma.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
168 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* gamma -
* Some support for gamma correction when reading and writing
* color map entries.
*
* Paul Haeberli - 1984
*
*/
#include "math.h"
#include "port.h"
#include "gl.h"
#include "stdio.h"
static fixup();
FILE *configopen();
float gammacorrect();
float ungammacorrect();
static unsigned char rgamtable[256];
static unsigned char ggamtable[256];
static unsigned char bgamtable[256];
static unsigned char rungamtable[256];
static unsigned char gungamtable[256];
static unsigned char bungamtable[256];
static short firsted;
#define MONITORGAMMA 2.8
gammapcolor(index,r,g,b)
int index, r, g, b;
{
mapcolor(index,r,g,b);
}
static makegamtables()
{
float gamval;
float val;
short i;
int rbal, gbal, bbal;
float grbal, ggbal, gbbal;
gamval = getgamma();
getcolorbal(&rbal,&gbal,&bbal);
grbal = gammacorrect(rbal/255.0,MONITORGAMMA);
ggbal = gammacorrect(gbal/255.0,MONITORGAMMA);
gbbal = gammacorrect(bbal/255.0,MONITORGAMMA);
for (i=0; i<256; i++) {
rgamtable[i] = 255.0*gammacorrect((rbal*i)/(255.0*255.0),gamval)+0.49;
ggamtable[i] = 255.0*gammacorrect((gbal*i)/(255.0*255.0),gamval)+0.49;
bgamtable[i] = 255.0*gammacorrect((bbal*i)/(255.0*255.0),gamval)+0.49;
}
bzero(rungamtable,256);
bzero(gungamtable,256);
bzero(bungamtable,256);
for (i=0; i<256; i++) {
rungamtable[rgamtable[i]] = i;
gungamtable[ggamtable[i]] = i;
bungamtable[bgamtable[i]] = i;
}
fixup(rungamtable);
fixup(gungamtable);
fixup(bungamtable);
}
static fixup(cptr)
unsigned char *cptr;
{
short i, lowval;
lowval = 0;
for (i=256; i--; ) {
if (*cptr == 0)
*cptr = lowval;
else
lowval = *cptr;
}
}
gamgetmcolor(index,r,g,b)
int index;
short *r, *g, *b;
{
getmcolor(index,r,g,b);
}
float gammacorrect(i,gamma)
float i, gamma;
{
return pow(i,1.0/gamma);
}
float ungammacorrect(i,gamma)
float i, gamma;
{
return pow(i,gamma);
}
newgamma()
{
firsted = firsted = 0;
}
newgamtables()
{
FILE *outf;
int i;
short rtab[256], gtab[256], btab[256];
if ((outf = configopen(".gamtables","w")) == 0) {
fprintf(stderr,"couldn't open .gamtables\n");
return;
}
makegamtables();
fwrite(rgamtable,256,1,outf);
fwrite(ggamtable,256,1,outf);
fwrite(bgamtable,256,1,outf);
fwrite(rungamtable,256,1,outf);
fwrite(gungamtable,256,1,outf);
fwrite(bungamtable,256,1,outf);
fclose(outf);
for(i=0; i<256; i++) {
rtab[i] = rgamtable[i];
gtab[i] = ggamtable[i];
btab[i] = bgamtable[i];
}
gammaramp(rtab,gtab,btab);
}
readgamtables()
{
FILE *inf;
if ((inf = configopen(".gamtables","r")) == 0) {
newgamtables();
if ((inf = configopen(".gamtables","r")) == 0) {
fprintf(stderr,"couldn't open .gamtables\n");
return;
}
}
fread(rgamtable,256,1,inf);
fread(ggamtable,256,1,inf);
fread(bgamtable,256,1,inf);
fread(rungamtable,256,1,inf);
fread(gungamtable,256,1,inf);
fread(bungamtable,256,1,inf);
fclose(inf);
}